package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IApplyScrapFeedbackView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.ApplyScrapFeedbackPresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ProduceTeamDailyFeedbackApplyScrapActivity extends BaseActivity implements IApplyScrapFeedbackView, IUploadPhotoView, BottomListDialog.OnItemDialogClick {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EditText edt_scrap_num, edt_scrap_reason, edt_scrap_day;
    private ImageView img_scrap_reason;
    private GridView gridView;
    private Button btn_submit;

    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean;

    private UploadPhotoPresenter uploadPhotoPresenter;
    private ApplyScrapFeedbackPresenter applyScrapFeedbackPresenter;

    private GridAdapter gridAdapter;
    private List<String> pathList = new ArrayList<>();
    private List<String> serverUrlList = new ArrayList<>();
    private String filePath;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_team_daily_feedback_apply_scrap);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_scrap_num = findViewById(R.id.edt_scrap_num);
        edt_scrap_reason = findViewById(R.id.edt_scrap_reason);
        img_scrap_reason = findViewById(R.id.img_scrap_reason);
        edt_scrap_day = findViewById(R.id.edt_scrap_day);
        gridView = findViewById(R.id.gridView_scrap);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.application_scrap));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartOrderBean = (WorkPartOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        uploadPhotoPresenter = new UploadPhotoPresenter(this);
        applyScrapFeedbackPresenter = new ApplyScrapFeedbackPresenter(this);


        pathList.add("paizhao");
        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(this);
        gridAdapter.setData(pathList);
        gridView.setAdapter(gridAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {

                String imgs = (String) parent.getItemAtPosition(position);
                if ("paizhao".equals(imgs)) {
                    showListDialog();
                } else {

                }
            }
        });
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_scrap_reason.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    private void submit() {
        if (TextUtils.isEmpty(edt_scrap_num.getText().toString())) {
            showToast(getResources().getString(R.string.input_return_num));
            return;
        } else if (TextUtils.isEmpty(edt_scrap_reason.getText().toString())) {
            showToast(getResources().getString(R.string.input_return_reason));
            return;
        } else if (TextUtils.isEmpty(edt_scrap_day.getText().toString())) {
            showToast(getResources().getString(R.string.input_scrap_day));
            return;
        } else {
            applyScrapFeedbackPresenter.applyScrapFeedback();
        }
    }

    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 报废原因
            case R.id.img_scrap_reason:
                break;
            // 提交
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case PhotoUtil.CAMERA:
                if (resultCode == RESULT_OK) {
                    File file = new File(PhotoUtil.filePath);
                    if (!file.exists()) {
                        file = new File(PhotoUtil.filePath);
                    }

                    filePath = PhotoUtil.CAMERA_SEVER + file.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(file.getAbsolutePath(), filePath);

                    if (pathList.contains("paizhao")) {
                        pathList.remove("paizhao");
                    }
                    if (pathList.size() < 3) {
                        pathList.add(filePath);
                        if (pathList.size() < 3)
                            pathList.add("paizhao");

                        gridAdapter.setData(pathList);
                        gridAdapter.notifyDataSetChanged();
                    }
                    uploadPhotoPresenter.uploadPhtoto();
                }
                break;

            case PhotoUtil.GALLERY:
                if (resultCode == RESULT_OK) {

                    String path = FileUtil.getRealPathFromURI(this, Uri.parse(data.getData() + ""));

                    if (pathList.contains("paizhao")) {
                        pathList.remove("paizhao");
                    }

                    if (pathList.size() < 3) {
                        pathList.add(path);
                        if (pathList.size() < 3)
                            pathList.add("paizhao");
                        gridAdapter.setData(pathList);
                        gridAdapter.notifyDataSetChanged();
                    }
                    File f = new File(path);
                    filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(path, filePath);

                    uploadPhotoPresenter.uploadPhtoto();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public int getAmount() {
        return Integer.parseInt(edt_scrap_num.getText().toString().trim());
    }

    @Override
    public String getContent() {
        return edt_scrap_reason.getText().toString();
    }

    @Override
    public List<String> getImgUrlList() {
        return serverUrlList;
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void applyScrapFeedbackSuccessed() {
        showToast("提交成功");
        EventBus.getDefault().post(new ProduceForTeamEvent());
        finish();
    }

    @Override
    public void applyScrapFeedbackFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public String getFilePath() {
        return filePath;
    }

    @Override
    public void upLoadPhotoSuccessed(String uri) {
        serverUrlList.add(uri);
    }

    @Override
    public void upLoadPhotoFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onItemDialogClick(DialogItemBean item) {
        switch (item.getmTextId()) {
            case R.string.camera: {
                PhotoUtil.startCamear(ProduceTeamDailyFeedbackApplyScrapActivity.this, null);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(ProduceTeamDailyFeedbackApplyScrapActivity.this, null);
                break;
            }
            default:
                break;
        }
    }
}
